//
//  HYFileDownloadViewModel.m
//  XPlayer
//
//  Created by liguoqing on 2023/10/12.
//

#import "HYFileDownloadViewModel.h"
#import "HYFileDownloadModel.h"
#import "HYFileDownloadItemViewModel.h"
#import "HYVideoViewModel.h"
#import <HYVideoPlayer/HYVideoPlayerConfig.h>
#import <HYVideoPlayer/HYVideoModel.h>

@interface HYFileDownloadViewModel ()

@property (nonatomic, strong) NSMutableArray *dataSoucre;

@property (nonatomic, strong) NSString *folderIdentification;

@end

@implementation HYFileDownloadViewModel

- (instancetype)init
{
    self = [super init];
    if(self){
        self.playSubject = [RACSubject subject];
        self.downCompletdSubject = [RACSubject subject];
    }
    return self;
}

- (instancetype)initWithFolderIdentification:(NSString *)folderIdentification
{
    self = [super init];
    if(self){
        self.folderIdentification = folderIdentification;
    }
    return self;
}

- (void)loadData
{
    [self.dataSoucre removeAllObjects];
    NSArray *downloads;
    if(self.folderIdentification){
        NSString *sql = [NSString stringWithFormat:@"select *from DOWNLOAD where folderIdentification = '%@' order by downloadDate DESC",self.folderIdentification];
        downloads = [HYFileDownloadModel fetchDownloadTaskWhere:sql];
    }else{
        downloads = [HYFileDownloadModel fetchAllDownloadTaskData];
    }
    //下载完成的数组
    NSMutableArray *downLoadCompletedArray = [NSMutableArray array];
    //未下载完成的数组
    NSMutableArray *downloadingArray = [NSMutableArray array];
    [downloads enumerateObjectsUsingBlock:^(HYFileDownloadModel *model, NSUInteger idx, BOOL * _Nonnull stop) {
        HYFileDownloadItemViewModel *itemVM = [[HYFileDownloadItemViewModel alloc]initWithDownloadModel:model];
        if (itemVM.downloadStatus == 1) {
            [downLoadCompletedArray hy_addObject:itemVM];
        }
        if (itemVM.downloadStatus == 0) {
            itemVM.filedownCompletdSubject = self.downCompletdSubject;
            [downloadingArray hy_addObject:itemVM];
        }
    }];
    NSDictionary *downLoadCompletedDic = @{@"title":NSLocalizedString(@"已缓存", nil),@"itemSource":downLoadCompletedArray};
    NSDictionary *downloadingDic = @{@"title":NSLocalizedString(@"正在缓存", nil),@"itemSource":downloadingArray};
    if (downloadingArray.count != 0) {
        [self.dataSoucre hy_addObject:downloadingDic];
    }
    if (downLoadCompletedArray.count != 0) {
        [self.dataSoucre hy_addObject:downLoadCompletedDic];
    }
}

- (NSInteger)numberOfSectionsInTableView
{
    return self.dataSoucre.count;
}

- (NSInteger)numberOfRowsInSection:(NSInteger)section
{
    NSDictionary *sectionInfo = self.dataSoucre[section];
    return [sectionInfo[@"itemSource"] count];
}

- (NSString *)headerTitleForHeaderInSection:(NSInteger)section
{
    NSDictionary *sectionInfo = self.dataSoucre[section];
    return sectionInfo[@"title"];
}
- (id)cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary *sectionInfo = self.dataSoucre[indexPath.section];
    NSArray *itemSource = sectionInfo[@"itemSource"];
    return itemSource[indexPath.row];
}

- (void)didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary *sectionInfo = self.dataSoucre[indexPath.section];
    NSArray *itemSource = sectionInfo[@"itemSource"];
    HYFileDownloadItemViewModel *itemVM = itemSource[indexPath.row];
    [self showDownloadFileWithItemVM:itemVM];
}

- (void)showDownloadFileWithItemVM:(HYFileDownloadItemViewModel *)itemVM
{
    if(itemVM.downloadTask.status == HYDownloadStatus_Completed){
        HYVideoModel *currentVideo = [[HYVideoModel alloc]init];
        currentVideo.videoTitle = itemVM.fileName;
        currentVideo.videoUrl = itemVM.fileUrl;
        currentVideo.videoImage = [UIImage imageNamed: [HYFileManager videoImageSavePathWithFileName:itemVM.imageName]];
        currentVideo.videoImagePath = itemVM.imageName;
        currentVideo.isAudio = itemVM.fileType;
        currentVideo.createDate = itemVM.createDate;
        currentVideo.isSelect = YES;
        HYVideoPlayerConfig *config = [[HYVideoPlayerConfig alloc]init];
        config.isBasePlayer = YES;
        config.currentVideoModel = currentVideo;
        config.videoSoucres = @[currentVideo];
        HYVideoViewModel *videoModel = [[HYVideoViewModel alloc]initWithFolderIdentification:itemVM.folderIdentification isPrivate:itemVM.isPrivate videoConfig:config];
        [self.playSubject sendNext:videoModel];
    }
}

- (BOOL)deleteDownloadTashAtIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary *sectionInfo = self.dataSoucre[indexPath.section];
    NSMutableArray *itemSource = sectionInfo[@"itemSource"];
    HYFileDownloadItemViewModel *itemVC = itemSource[indexPath.row];
    BOOL result = [itemVC deleteDownload];
    if(result){
        [itemSource removeObject:itemVC];
    }
    return result;
}

- (NSMutableArray *)dataSoucre{
    if(!_dataSoucre){
        _dataSoucre = [NSMutableArray array];
    }
    return _dataSoucre;
}

@end
